const Service = require("egg").Service;

class NewsService extends Service {

    async getList() {
        const { ctx, app } = this;
        let {
            receive_id,
            sender_id
        } = ctx.query;
        console.log(receive_id,
            sender_id);
        let data = await app.mysql.select("news", { where: { receive_id: [receive_id, sender_id], sender_id: [receive_id, sender_id] } });
        data.sort((a, b) => {
            return a.sender_time - b.sender_time
        })
        return {
            total: data.length,
            data
        }
    }
    async getGoodList() {
        const { ctx, app } = this;
        let {
            user_id
        } = ctx.query;
        let data = await app.mysql.select("goodFriend", { where: { user_id }, orders: [['update_time', 'asc']] });
        let notice = await app.mysql.select("notice", { orders: [['create_time', 'asc']] })
        return {
            total: data.length,
            data,
            notice
        }
    }
    //删除
    async delNotice() {
        const { ctx, app } = this;
        const { id } = JSON.parse(ctx.query.params);
        console.log(id, 123);
        // let res = await app.mysql.delete("orders", { id });
        return "1123";
    }
    //发送消息
    async sendNews() {
        const { ctx, app } = this;
        const item = ctx.request.body
        let res = await app.mysql.insert("news", { ...item, sender_time: Date.now() });
        let hove = await app.mysql.get("goodFriend", { goodFriend_id: item.sender_id })
        if (hove) {
            const quantity = hove.quantity + 1;
            await app.mysql.update("goodFriend", { quantity, newNews: item.sender_context, id: hove.id })
        } else {
            await app.mysql.insert("goodFriend", {
                goodFriend_id: item.sender_id,
                goodFriend_name: item.sender_name,
                update_time: Date.now(),
                newNews: item.sender_context,
                user_id: item.receive_id,
                goodFriend_headImg: item.headImg
            })
        }
        return res
    }
}
module.exports = NewsService;